{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73bd968b-d970-4a05-94ef-4e7abf990827",
   "metadata": {},
   "source": [
    "Chapter 02\n",
    "\n",
    "# 矩阵乘法\n",
    "Book_3《数学要素》 | 鸢尾花书：从加减乘除到机器学习 (第二版)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2dcd417e-6f44-45f1-a6ee-94251d7ff9b6",
   "metadata": {},
   "source": [
    "该代码使用`numpy`库计算两个矩阵的乘积，并逐项展示了每个元素的计算过程。矩阵乘法是通过行与列的对应元素相乘并求和得到结果矩阵中的每个元素。\n",
    "\n",
    "### 代码细节\n",
    "\n",
    "1. **定义矩阵**：\n",
    "   - 定义矩阵 $A$ 和 $B$：\n",
    "   \n",
    "     $$\n",
    "     A = \\begin{bmatrix} 1 & 2 \\\\ 3 & 4 \\end{bmatrix}, \\quad B = \\begin{bmatrix} 4 & 2 \\\\ 3 & 1 \\end{bmatrix}\n",
    "     $$\n",
    "\n",
    "2. **矩阵乘法计算**：\n",
    "   - 计算$A$和$B$的矩阵乘积$C$，结果矩阵$C$的每个元素$C[i,j]$为$A$的第$i$行与$B$的第$j$列的点积。\n",
    "   - 矩阵乘积的计算公式为：\n",
    "\n",
    "     $$\n",
    "     C = A \\times B = \\begin{bmatrix} A[0,0] \\cdot B[0,0] + A[0,1] \\cdot B[1,0] & A[0,0] \\cdot B[0,1] + A[0,1] \\cdot B[1,1] \\\\ A[1,0] \\cdot B[0,0] + A[1,1] \\cdot B[1,0] & A[1,0] \\cdot B[0,1] + A[1,1] \\cdot B[1,1] \\end{bmatrix} = \\begin{bmatrix} 10 & 4 \\\\ 24 & 10 \\end{bmatrix}\n",
    "     $$\n",
    "\n",
    "3. **逐项计算过程**：\n",
    "   - 第1行、第1列元素：\n",
    "\n",
    "     $$\n",
    "     C[0,0] = A[0,0] \\times B[0,0] + A[0,1] \\times B[1,0] = 1 \\times 4 + 2 \\times 3 = 10\n",
    "     $$\n",
    "   \n",
    "   - 第1行、第2列元素：\n",
    "\n",
    "     $$\n",
    "     C[0,1] = A[0,0] \\times B[0,1] + A[0,1] \\times B[1,1] = 1 \\times 2 + 2 \\times 1 = 4\n",
    "     $$\n",
    "   \n",
    "   - 第2行、第1列元素：\n",
    "\n",
    "     $$\n",
    "     C[1,0] = A[1,0] \\times B[0,0] + A[1,1] \\times B[1,0] = 3 \\times 4 + 4 \\times 3 = 24\n",
    "     $$\n",
    "   \n",
    "   - 第2行、第2列元素：\n",
    "\n",
    "     $$\n",
    "     C[1,1] = A[1,0] \\times B[0,1] + A[1,1] \\times B[1,1] = 3 \\times 2 + 4 \\times 1 = 10\n",
    "     $$\n",
    "\n",
    "### 总结\n",
    "\n",
    "此代码通过计算矩阵乘法，展示了如何逐项计算结果矩阵$C$中的每个元素。矩阵乘法在许多应用中至关重要，尤其是在线性变换和数据处理领域。最终的结果矩阵$C$表示了矩阵$A$和$B$之间的乘积关系。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29274328-4774-476c-bbcf-8004a9df93e8",
   "metadata": {},
   "source": [
    "## 定义矩阵并计算矩阵乘积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1eb29c93-4a55-46ed-8072-c2f5c451bd4b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4]])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np  # 导入numpy库\n",
    "\n",
    "A = np.array([[1, 2],  # 定义矩阵A\n",
    "              [3, 4]])\n",
    "A  # 输出矩阵A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "25cf8260-eec8-4a6e-88d0-14d57d8a687f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 2],\n",
       "       [3, 1]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B = np.array([[4, 2],  # 定义矩阵B\n",
    "              [3, 1]])\n",
    "B  # 输出矩阵B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "adbceb53-fecc-47d0-b50f-84fb80528ee8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[10,  4],\n",
       "       [24, 10]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C = A @ B  # 计算矩阵A和B的乘积\n",
    "C  # 输出矩阵乘法结果C"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b90bcc99-505f-42ae-9bac-75bf30ae7dd3",
   "metadata": {},
   "source": [
    "## 逐元素计算矩阵乘法的结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "07fe857b-ef15-4263-ba47-c5d8cc2ff54c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[10]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A[[0]] @ B[:, [0]]  # 第1行和第1列的矩阵乘法结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "0fa92418-cc51-4678-8d79-d3a096574baf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A[[0]] @ B[:, [1]]  # 第1行和第2列的矩阵乘法结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c3188767-a92d-4001-bd13-5646c4336344",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[24]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A[[1]] @ B[:, [0]]  # 第2行和第1列的矩阵乘法结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b5f5d6fd-76ca-40b3-a03f-2f4e585b436d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[10]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A[[1]] @ B[:, [1]]  # 第2行和第2列的矩阵乘法结果"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
